//
//  OBMomentPhotosView.m
//  WeChat
//
//  Created by 黄柳姣 on 2018/5/3.
//  Copyright © 2018年 obally. All rights reserved.
//

#import "OBMomentPhotosView.h"
#import "OBMomentItemViewModel.h"
#import "OBImageModel.h"
#import "OBMomentPhotoView.h"

@interface OBMomentPhotosView()
/// viewModel
@property (nonatomic, readwrite, strong) OBMomentPhotosView *viewModel;
@end
@implementation OBMomentPhotosView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //预先创建9个图片空间 避免动态创建
        for (int i = 0; i < OBMomentPhotosMaxCount; i++) {
            OBMomentPhotoView *photoView = [[OBMomentPhotoView alloc]init];
            photoView.backgroundColor = self.backgroundColor;
            photoView.hidden = YES;
            photoView.tag = i;
            [self addSubview:photoView];
            // 添加手势监听器（一个手势监听器 只能 监听对应的一个view）
            UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] init];
            [recognizer addTarget:self action:@selector(_tapPhoto:)];
            [photoView addGestureRecognizer:recognizer];
        }
    }
    return self;
}
///bind data
- (void)bindViewModel:(OBMomentItemViewModel *)viewModel
{
    self.viewModel = viewModel;
    CGFloat photoViewWH = OBMomentPhotosViewItemWidth();
    ///设置显示or隐藏布局
    NSUInteger count = viewModel.picInfos.count;
    if (count == 0) {
        [self _hideAllImageViews];
    }
    int maxCols = OBMomentMaxCols(count);
    for (int i = 0; i<OBMomentPhotosMaxCount; i++) {
        OBMomentPhotoView *photoView = self.subviews[i];
        if (i < count) {
            ///显示隐藏
            photoView.hidden = NO;
            if (count == 1) {
                ///一张图情况
                photoView.frame = self.bounds;
            } else {
                ///其他情况
                photoView.mh_width = photoViewWH;
                photoView.mh_height = photoViewWH;
                photoView.mh_x = (i % maxCols) * (photoViewWH + OBMomentPhotosViewItemInnerMargin);
                photoView.mh_y = (i / maxCols) * (photoViewWH + OBMomentPhotosViewItemInnerMargin);
            }
            ///绑定数据
            [photoView bindViewModel:viewModel.picInfos[i]];
            
        } else {
            ///隐藏
            photoView.hidden = YES;
        }
    }
}

/// 隐藏所有图片
- (void)_hideAllImageViews {
    for (OBMomentPhotoView *imageView in self.subviews) {
        imageView.hidden = YES;
    }
}
#pragma mark - 事件处理
- (void)_tapPhoto:(UITapGestureRecognizer *)sender
{
    
}
@end
